package com.example.finalhomework.cardgame

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.example.finalhomework.HomeScreen
import com.example.finalhomework.R
import com.example.finalhomework.cardgame.ui.AdvancedCardGameScreen
import com.example.finalhomework.cardgame.ui.CardGameScreen
import com.example.finalhomework.cardgame.ui.UserInfo
import com.example.finalhomework.cardgame.viewmodel.AdvancedCardGameViewModel
import com.example.finalhomework.cardgame.viewmodel.CardGameViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen_cardgame(navController: NavController) {
    // 创建一个NavController用于导航
    val navController = rememberNavController()
    val context = LocalContext.current
    // 获取CardGameViewModel和AdvancedCardGameViewModel实例
    val cardGameViewModel: CardGameViewModel = viewModel()
    val advancedCardGameViewModel: AdvancedCardGameViewModel = viewModel()

    // 记录当前路由
    val currentRoute = remember { mutableStateOf("PersonalInfo") }

    // 添加导航目的地改变监听器
    navController.addOnDestinationChangedListener { _, destination, _ ->
        currentRoute.value = destination.route ?: "Update Information"
        Log.d("MainScreen_cardgame", "Navigated to ${currentRoute.value}")
    }

    // Scaffold用于提供一个基础的Material Design布局结构
    Scaffold(
        topBar = {
            CenterAlignedTopAppBar(
                title = {
                    Text(
                        text = when (currentRoute.value) {
                            "PersonalInfo" -> "Personal Information"
                            "CardGame" -> "Card Game"
                            "AdvancedCardGame" -> "Advanced Card Game"
                            else -> "Update Information"
                        },
                        color = Color.White,
                        modifier = Modifier
                            .fillMaxSize()
                            .wrapContentSize(Alignment.Center),
                        textAlign = TextAlign.Center,
                        style = TextStyle(fontSize = 16.sp)
                    )
                },
                colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primary
                ),
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.dp)
            )
        },
        bottomBar = { BottomNavigationBar(navController) } // 底部导航栏
    ) { innerPadding ->
        // 使用Box布局来填充整个屏幕，并应用内边距
        Box(modifier = Modifier
            .fillMaxSize()
            .padding(innerPadding)
            .background(Color.White)
        ) {
            // NavHost用于管理导航目的地
            NavHost(navController = navController, startDestination = "PersonalInfo", modifier = Modifier.fillMaxSize()) {
                // 定义导航目的地为AdvancedCardGameScreen
                composable("AdvancedCardGame") {
                    Log.d("MainScreen_cardgame", "Navigating to AdvancedCardGameScreen")
                    AdvancedCardGameScreen(navController, advancedCardGameViewModel) }
                // 定义导航目的地为CardGameScreen
                composable("CardGame") {
                    Log.d("MainScreen_cardgame", "Navigating to CardGameScreen")
                    CardGameScreen(navController, cardGameViewModel) }
                // 定义导航目的地为PersonalInfoScreen
                composable("PersonalInfo") {
                    Log.d("MainScreen_cardgame", "Navigating to PersonalInfoScreen")
                    PersonalInfoScreen(navController) }
                // 定义导航目的地为UpdateInfoScreen，并传递参数
                composable(
                    "UpdateInfo/{name}/{gender}/{studentId}/{phone}/{email}/{hobbies}/{avatarUri}",
                    arguments = listOf(
                        navArgument("name") { type = NavType.StringType },
                        navArgument("gender") { type = NavType.StringType },
                        navArgument("studentId") { type = NavType.StringType },
                        navArgument("phone") { type = NavType.StringType },
                        navArgument("email") { type = NavType.StringType },
                        navArgument("hobbies") { type = NavType.StringType },
                        navArgument("avatarUri") { type = NavType.StringType } // 修改为 StringType
                    )
                ) { backStackEntry ->
                    // 获取传递的参数
                    val name = backStackEntry.arguments?.getString("name") ?: ""
                    val gender = backStackEntry.arguments?.getString("gender") ?: ""
                    val studentId = backStackEntry.arguments?.getString("studentId") ?: ""
                    val phone = backStackEntry.arguments?.getString("phone") ?: ""
                    val email = backStackEntry.arguments?.getString("email") ?: ""
                    val hobbies = backStackEntry.arguments?.getString("hobbies") ?: ""
                    val avatarUri = backStackEntry.arguments?.getString("avatarUri") ?: ""
                    val userInfo = UserInfo(name, gender, studentId, phone, email, hobbies, avatarUri, 0, 0)
                    Log.d("MainScreen_cardgame", "Navigating to UpdateInfoScreen with userInfo: $userInfo")
                    // 显示UpdateInfoScreen，并传递userInfo
                    UpdateInfoScreen(navController, context, userInfo) { updatedUserInfo ->
                        Log.d("MainScreen_cardgame", "User info updated: $updatedUserInfo")
                    }
                }
            }
        }
    }
}

// 定义底部导航栏项目的数据类
data class BottomNavItem(val route: String, val icon: Int, val label: String)

@Preview
@Composable
fun PreviewMainScreen_cardgame() {
    MainScreen_cardgame(navController = rememberNavController())
}